package com.appointment.util;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.appointment.dao.AdminDao;
import com.appointment.entity.Admin;

public class LoginInterceptor extends HandlerInterceptorAdapter {
	
	@Resource
	AdminDao admindao;
	
	private static final String[] IGNORE_URI = {"/login.jsp", "/login", "/tologin","backui/","frontui/"};
	 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean flag = false;
        String url = request.getRequestURL().toString();
        System.out.println(">>>: " + url);
        for (String s : IGNORE_URI) {
            if (url.contains(s)) {
                flag = true;
                break;
            }
        }
        if (!flag) {
        	Admin admin = (Admin) request.getSession().getAttribute("admin");
        	if(admin==null){
        		flag = false;
        		response.sendRedirect("tologin");
        		return false;
        	}
        	Admin admins = admindao.findAdminByUsernameAndPsw(admin);
        	if(admins==null){
        		flag = false;
        		response.sendRedirect("/admin/tologin");
        	}
            //T_supplier_user user = LoginController.getLoginUser(request);
            //if (user != null) flag = true;
        }
        return true;
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }
}
